package com.kingdee.eas.custom.hostelmgr.basedata.app.di;

import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Locale;

import com.kingdee.bos.Context;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.person.Genders;
import com.kingdee.eas.custom.hostelmgr.basedata.CategoryEnum;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicFactory;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicInfo;
import com.kingdee.eas.custom.hostelmgr.basedata.IHostelBasic;
import com.kingdee.eas.custom.hostelmgr.basedata.ScheduleEnum;
import com.kingdee.eas.custom.hostelmgr.basedata.TypeEnum;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.tools.datatask.AbstractDataTransmission;
import com.kingdee.eas.tools.datatask.core.TaskExternalException;
import com.kingdee.eas.tools.datatask.runtime.DataToken;
import com.kingdee.eas.util.ResourceBase;
import com.kingdee.jdbc.rowset.IRowSet;

public class HostelBasicDI extends AbstractDataTransmission {

	protected String RESOURCE_FILE_NAME = "com.kingdee.eas.custom.hostelmgr.basedata.ImportResource";

	@Override
	protected ICoreBase getController(Context cxt) throws TaskExternalException {
		ICoreBase iCoreBase = null;
		try {
			iCoreBase = getBizInterface(cxt);
		} catch (Exception e) {
			throw new TaskExternalException(e.getMessage(), e);
		}

		return iCoreBase;
	}

	public CoreBaseInfo transmit(Hashtable hsData, Context ctx)
			throws TaskExternalException {
		try {
			CoreBaseInfo newData = createNewData(hsData, ctx);
			injectIDWhenUpdate(newData, ctx);
			return newData;
		} catch (Exception e) {
			throw new TaskExternalException(e.getMessage(), e);
		}

	}

	public Hashtable exportTransmit(IRowSet rs, Context ctx)
			throws TaskExternalException {
		Hashtable result = new Hashtable();
		try {
			result.put("FNumber", avoidNull(rs.getString("number")));
			result.put("FName", avoidNull(rs.getString("name")));
			result.put("CFType", avoidNull(rs.getString("type")));
			result.put("CFOtherName", avoidNull(rs.getString("otherName")));
			result.put("CFTelephone", avoidNull(rs.getString("telephone")));
			result.put("CFGender", avoidNull(rs.getString("gender")));
			result.put("CFCategory", avoidNull(rs.getString("category")));
			result.put("CFSchedule", avoidNull(rs.getString("schedule")));
			result.put("CFBedNum", avoidNull(rs.getString("bedNum")));
			result.put("CFParentNum", avoidNull(this.getHostelBasicInfo(ctx, rs
					.getString("number"))));
		} catch (Exception e) {
			e.printStackTrace();
			Locale currentLocale = ctx.getLocale();
			String message = ResourceBase.getString(this.RESOURCE_FILE_NAME,
					"DB_ACCESS_ERROR", currentLocale);

			throw new TaskExternalException(message, e);
		}

		return result;
	}

	public String getExportQueryInfo(Context ctx) {
		return "com.kingdee.eas.custom.hostelmgr.basedata.app.HostelBasicQuery";
	}

	protected IHostelBasic getBizInterface(Context ctx) throws Exception {
		return HostelBasicFactory.getLocalInstance(ctx);
	}

	protected CoreBaseInfo createNewData(Hashtable hsData, Context ctx)
			throws Exception {
		String cfType = this.getFieldValue(hsData, "CFType");
		String cfOtherName = this.getFieldValue(hsData, "CFOtherName");
		String cfTelephone = this.getFieldValue(hsData, "CFTelephone");
		String cfGender = this.getFieldValue(hsData, "CFGender");
		String cfCategory = this.getFieldValue(hsData, "CFCategory");
		String cfSchedule = this.getFieldValue(hsData, "CFSchedule");
		String cfBedNum = this.getFieldValue(hsData, "CFBedNum");
		String cfParentNum = this.getFieldValue(hsData, "CFParentNum");
		String fName = this.getFieldValue(hsData, "FName");
		String fNumber = this.getFieldValue(hsData, "FNumber");

		HostelBasicInfo hostelBasicInfo = new HostelBasicInfo();

		this.checkRequeiredField(ctx, "CFType", cfType);
		TypeEnum type = TypeEnum.getEnum(Integer.valueOf(cfType));
		hostelBasicInfo.setType(type);

		this.checkRequeiredField(ctx, "FName", fName);
		hostelBasicInfo.setName(fName);

		this.checkRequeiredField(ctx, "FNumber", fNumber);
		hostelBasicInfo.setNumber(fNumber);

		if (type.equals(TypeEnum.BUILDING)) {

			if (cfOtherName != null && cfOtherName.trim().length() != 0) {
				hostelBasicInfo.setOtherName(cfOtherName);
			}

		} else if (type.equals(TypeEnum.UNIT)) {

		} else if (type.equals(TypeEnum.FLOOR)) {
			this.checkRequeiredField(ctx, "CFGender", cfGender);
			Genders gender = Genders.getEnum(Integer.valueOf(cfGender));
			hostelBasicInfo.setGender(gender);
		} else if (type.equals(TypeEnum.ROOM)) {
			this.checkRequeiredField(ctx, "CFCategory", cfCategory);
			CategoryEnum category = CategoryEnum.getEnum(Integer
					.valueOf(cfCategory));
			hostelBasicInfo.setCategory(category);

			this.checkRequeiredField(ctx, "CFSchedule", cfSchedule);
			ScheduleEnum scheduleEnum = ScheduleEnum.getEnum(Integer
					.valueOf(cfSchedule));
			hostelBasicInfo.setSchedule(scheduleEnum);

			this.checkRequeiredField(ctx, "CFBedNum", cfBedNum);
			int bedNum = Integer.valueOf(cfBedNum);
			hostelBasicInfo.setBedNum(bedNum);

			if (cfTelephone != null && cfTelephone.trim().length() != 0) {
				hostelBasicInfo.setTelephone(cfTelephone);
			}
		} else if (type.equals(TypeEnum.BED)) {

		}

		hostelBasicInfo.setParent(this.getHostelBasicInfo(ctx, cfParentNum));
		injectIDWhenUpdate(hostelBasicInfo, ctx);
		return hostelBasicInfo;
	}

	protected HostelBasicInfo getHostelBasicInfo(Context ctx, String number)
			throws Exception {
		String oql = "where number = '" + number + "'";
		HostelBasicInfo hostelBasicInfo = getBizInterface(ctx)
				.getHostelBasicInfo(oql.toString());
		return hostelBasicInfo;
	}

	protected void injectIDWhenUpdate(CoreBaseInfo newData, Context ctx)
			throws Exception {
		if (((Integer) getContextParameter().get("DATATASKMODE")).intValue() == 16) {
			BOSUuid id = getID(newData, ctx);
			if (id != null)
				newData.setId(id);
		}
	}

	protected BOSUuid getID(CoreBaseInfo newData, Context ctx) throws Exception {
		String propertyName = getPropertyNameWithWhichTheIDCanBeFound();
		String propertyValue = newData.get(propertyName).toString();

		StringBuffer oql = new StringBuffer(50);
		oql.append("select id where ").append(propertyName).append(" = '")
				.append(propertyValue).append("'");

		BOSUuid id = null;
		if (getBizInterface(ctx).exists(oql.toString())) {
			id = getBizInterface(ctx).getValue(oql.toString()).getId();
		}

		return id;
	}

	protected String getPropertyNameWithWhichTheIDCanBeFound() throws Exception {
		return "number";
	}

	protected void checkRequeiredField(Context ctx, String field, String value)
			throws Exception {
		if ((value == null) || (value.trim().length() == 0)) {
			StringBuffer message = new StringBuffer();
			Locale currentLocale = ctx.getLocale();
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME,
					"FIELD", currentLocale));
			message.append(field);
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME,
					"NOT_NULL", currentLocale));

			throw new Exception(message.toString());
		}
	}

	protected String getFieldValue(Hashtable hsData, String key) {
		return ((String) ((DataToken) hsData.get(key)).data).trim();
	}

	protected String avoidNull(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return "";
	}
}
